﻿using CPPEI.Coolzon.Metadata;
using CPPEI.Coolzon.UI;
using CPPEI.Coolzon.UI.Data;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace CPPEI.Coolzon.BrowserApp.MainViewControl
{
    /// <summary>
    /// SceneImmediate.xaml 的交互逻辑
    /// </summary>
    public partial class SceneImmediate : UserControl
    {
        public List<Scene> Scenes { get; set; }
        public bool FromImmediate;
        public Scene CurrentScene;
        public SceneImmediate()
        {
            InitializeComponent();
            string dayOfWeek = System.Globalization.CultureInfo.CurrentCulture.DateTimeFormat.GetDayName(DateTime.Now.DayOfWeek);

            tbCurrentDate.Text = DateTime.Now.ToString("yyyy年MM月dd日 ") + dayOfWeek;
        }
        public void BindSceneImmediate()
        {
            tvList.ItemsSource = null;
            if (Scenes != null && Scenes.Count() > 0)
            {
                tvList.ItemsSource = Scenes;
            }
        }

        private void tvList_SelectedItemChanged(object sender, RoutedPropertyChangedEventArgs<object> e)
        {
            if (tvList.SelectedItem != null)
            {
                Scene scene = tvList.SelectedItem as Scene;
                FromImmediate = true;
                //Common.EventBus.Instance.Publish(new UI.Event.OpenSceneEvent(SwitchMode.Replace, scene.Name, scene.Name, null));
                Event.Core.CoreEventBus.Publish(new Event.Core.CoreOpenSceneEvent(SwitchMode.Replace, scene.Name, scene.Name, null));
            }
        }

        private void tvList_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            if (tvList.SelectedItem != null)
            {
                Scene scene = tvList.SelectedItem as Scene;
                FromImmediate = true;
                //Common.EventBus.Instance.Publish(new UI.Event.OpenSceneEvent(SwitchMode.Replace, scene.Name, scene.Name, null));
                Event.Core.CoreEventBus.Publish(new Event.Core.CoreOpenSceneEvent(SwitchMode.Replace, scene.Name, scene.Name, null));
            }
        }

        #region implement ISceneDisplayChangedEventReceiver

        /// <summary>
        /// 场景打开之后
        /// </summary>
        [Common.Subscribe]
        public void OnOpened(Event.Core.CoreOpenSceneEvent @event)
        {
            if (!FromImmediate && @event.SwitchMode != SwitchMode.Pop && (@event.FrameName == null || !@event.FrameName.Equals("root")))
            {
                Scene scene = SolutionUtil.GetScene(UIContext.Current.Solution, @event.SceneName);
                if (scene.IsShowInNavigation)
                {
                    if (scene.Scenes != null)
                    {
                        Scenes = scene.Scenes;
                        BindSceneImmediate();
                    }
                }
            }
            FromImmediate = false;
        }

        /// <summary>
        /// 场景关闭之后
        /// </summary>
        [Common.Subscribe]
        public void OnHidden(Event.Core.CoreOpenSceneCompletedEvent @event)
        {
        }

        #endregion
    }
}
